from django.shortcuts import render
from view_study.models import *
from django.core.files.storage import FileSystemStorage  # django提供保存图片类
from django.http import HttpResponse
from advance_view.settings import STATICFILES_DIRS
import os


# Create your views here.


def send_pic(request):
    if request.method == "GET":
        return render(request, "post_photo.html")
    if request.method == "POST":
        # img = request.FILES.get("pic")  # 获取到上传的照片
        # f = FileSystemStorage()
        # save_path = f"post/{img.name}"
        # path = f.save(save_path, img)
        # p = Post.objects.get(id=2)
        # p.photo = path
        # p.save()
        # user = Post.objects.all()
        # return render(request, "img_re.html", context={"user_list": user})
        file = request.FILES.get('file')
        if not file:
            return HttpResponse("请上传您的文件")
        # 写入

        # f = FileSystemStorage()
        # save_path = "post/file/"
        # path = f.save(save_path, file)
        # return HttpResponse('写入成功1!')
        f = open(os.path.join(STATICFILES_DIRS[0], 'file'), "wb")
        for chuck in file.chunks():  # 分块写入文件
            f.write(chuck)
        f.close()
        return HttpResponse('写入成功1!')
